package cn.sh.yhk.configure.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SessionFilter implements Filter {

    private static Logger log = LoggerFactory.getLogger(SessionFilter.class);
    private FilterConfig config;

    @Override
    public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {

        this.config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        filterChain.doFilter(servletRequest, servletResponse);
//        //跨域
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Headers",
//                "User-Agent,Origin,Cache-Control,Content-type,Date,Server,withCredentials,AccessToken");
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
//        response.setHeader("Access-Control-Max-Age", "1209600");
//        response.setHeader("Access-Control-Expose-Headers", "accesstoken");
//        response.setHeader("Access-Control-Request-Headers", "accesstoken");
//        response.setHeader("Expires", "-1");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setHeader("pragma", "no-cache");
//        //session过滤关闭
//        HttpSession session = request.getSession();
//        String uri = request.getServletPath();
//        String urls = config.getInitParameter("excludeUrl");
//        if (isExcludedUri(urls, uri)) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else {
//            SUserVo user = (SUserVo) session.getAttribute(Common.SESSION_LOGIN_USER_INFO);
//            if (user == null) {
//                response.setContentType("text/json; charset=UTF-8");
//                response.setCharacterEncoding("utf-8");
//                response.setStatus(401);
//                PrintWriter out = response.getWriter();
//                out.print(JSONObject.toJSON(BasicVo.fail("用户未登录")));
//                out.flush();
//                out.close();
//            } else {
//                filterChain.doFilter(servletRequest, servletResponse);
//            }
//        }
        //filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        this.config = null;
    }

    private boolean isExcludedUri(String urls, String uri) {
        for (String ex : urls.split(",")) {
            if (uri.indexOf(ex) > 0 || uri.equals(ex)) {
                return true;
            }
        }
        return false;

    }
}